package itsm.isperp.module.service.individuation;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.dto.incident.IcdIncidentPropertyDTO;
import itsm.isperp.module.entity.individuation.IddIndividuationProperty;
import itsm.isperp.module.repository.individuation.IddIndividuationPropertyMapper;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2016-07-21 18:06
 */
@Service
public class IddIndividuationPropertyService extends BaseService<IddIndividuationProperty> {

	@Autowired
	protected IddIndividuationPropertyMapper IddIndividuationPropertyMapper;

	@Override
	public IddIndividuationPropertyMapper getRepository() {
		return this.IddIndividuationPropertyMapper;
	}

	public List<IddIndividuationProperty> findByRelationId(String relationId) {
		return IddIndividuationPropertyMapper.findByRelationId(relationId);
	}

	public void deleteByRelationId(String relationId) {
		IddIndividuationPropertyMapper.deleteByRelationId(relationId);
	}

	public void deleteByRelationIdAndCategoryPropertyId(String relationId, String categoryPropertyId) {
		IddIndividuationPropertyMapper.deleteByRelationIdAndCategoryPropertyId(relationId, categoryPropertyId);
	}

	public IddIndividuationProperty findByRelationIdAndPropertyId(String relationId, String propertyId) {
		return IddIndividuationPropertyMapper.findByRelationIdAndPropertyId(relationId, propertyId);
	}

	public List<IddIndividuationProperty> findByRelationIdAndPropertyIds(String relationId, String... propertyIds) {
		return IddIndividuationPropertyMapper.findByRelationIdAndPropertyIds(relationId, propertyIds);
	}

	public List<IddIndividuationProperty> findByRelationIdsAndPropetyId(String[] relationIdArr, String propertyId) {
		if (relationIdArr == null || relationIdArr.length == 0) {
			return null;
		}

		return this.IddIndividuationPropertyMapper.findByRelationIdsAndPropertyId(relationIdArr, propertyId);
	}

	public List<IcdIncidentPropertyDTO> findDatailByRelationId(String relationId) {
		return this.IddIndividuationPropertyMapper.findDetailByRelationId(relationId);
	}

	public void deleteByRelationIdAndProperties(String relationId, List<IddIndividuationProperty> properties) {
		IddIndividuationPropertyMapper.deleteByRelationIdAndProperties(relationId, properties);
	}

	public List<Map<String, Object>> getProperty(String relationId, String categoryId, String status, String creator) {
		JqGridRequest req = new JqGridRequest();
		req.setPageable(false);
		String allSecrecyColumnsId = "";

		// 如果当前状态设置了保密字段，则不返回值

		// 获取所有保密字段
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		String sql = "select secrecy_columns from bpm_process_setting where  relate_form_id='" + categoryId + "' and secrecy_columns is not null";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> s : list) {
			String secrecy = s.get("secrecy_columns").toString();
			String ts[] = secrecy.split(",");
			for (int i = 0; i < ts.length; i++) {
				allSecrecyColumnsId += "'"+ts[i] + "',";
			}
		}

		IUser user = ContextHolder.getLoginUser();
		if (user.getUsername().equals(creator) && StringUtils.isNotBlank(allSecrecyColumnsId)) {
			allSecrecyColumnsId = allSecrecyColumnsId.substring(0, allSecrecyColumnsId.length() - 1);
		//	req.addFilterRule("category_property_id", JqGridFilterRuleOp.ni, "'"+allSecrecyColumnsId+"'");
			req.setSearchString(" category_property_id not in("+allSecrecyColumnsId+")");
		}

		req.addFilterRule("relation_id", JqGridFilterRuleOp.eq, relationId); // 配置项ID
		return IddIndividuationPropertyMapper.findAllDetailByRequest(req).getContent();
	}

}
